package com.example.trb.controller.work;

import com.example.trb.dto.ObjectDTO;
import com.example.trb.entity.User;
import com.example.trb.entity.WorkExperience;
import com.example.trb.service.work.WorkExperienceService;
import com.example.trb.util.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@Slf4j
public class WorkExperienceController {
    @Autowired
    WorkExperienceService weService;
    @Value("${session_user}")
    String userKey;

    @GetMapping("/workexperience/list")
    public CommonResult listWorkExperience(HttpSession session) {
        User user = (User) session.getAttribute(userKey);
        ObjectDTO dto = weService.listByUserId(user.getId());
        return CommonResult.success(dto.getData());
    }

    @PutMapping("/workexperience")
    public CommonResult putWorkExperience(@RequestBody @Validated WorkExperience data) {
        weService.update(data);
        return CommonResult.success(null);
    }

    @DeleteMapping("/workexperience")
    public CommonResult deleteWorkExperience(@RequestBody WorkExperience data) {
        weService.removeById(data.getId());
        return CommonResult.success(null);
    }


    @PostMapping("/workexperience/status")
    public CommonResult status(@RequestParam("id") Integer id,@RequestParam("status") String status)  {
        weService.updateStatusById(id,status);
        return CommonResult.success(null);
    }

    @GetMapping("/workexperience")
    public CommonResult getWorkExperience(@RequestParam("id") Integer id) {
        ObjectDTO dto = weService.getById(id);
        return CommonResult.success(dto.getData());
    }

    @PostMapping("/workexperience")
    public CommonResult addWorkExperience(
            @RequestBody @Validated WorkExperience data,
            HttpSession session
    ) {
        User user = (User) session.getAttribute(userKey);
        data.setUserId(user.getId());
        ObjectDTO dto = weService.save(data);
        return CommonResult.success(dto.getData());
    }
}
